package cc.douquan.com.doutu.view.fragment;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import cc.douquan.com.doutu.R;
import cc.douquan.com.doutu.adapter.FindPictureAdapter;
import cc.douquan.com.doutu.delegate.FindDelegate;
import cc.douquan.com.doutu.entity.ClassifyEntity;
import cc.douquan.com.doutu.entity.ItemData;
import cc.douquan.com.doutu.model.FindImgModel;
import cc.douquan.com.doutu.model.FindImgModelImpl;
import cc.douquan.com.doutu.model.HomeImgEntity;
import cc.douquan.com.doutu.model.OnNetRequestListener;
import cc.douquan.com.doutu.mvp_frame.presenter.FragmentPresenter;
import cc.douquan.com.doutu.utils.DataSever;
import cc.douquan.com.doutu.utils.ToastUtils;

import static android.nfc.tech.MifareUltralight.PAGE_SIZE;

/**
 * Created by qef on 2016/8/27.
 */
public class FindFragment extends FragmentPresenter<FindDelegate> {
    private FindPictureAdapter mAdapter;
    private List<ItemData> mNews;
    private int mPageNum = 1;
    private FindImgModel findImgModel;
    private int totalpage = 48;

    public static FindFragment newInstance() {
        FindFragment findFragment = new FindFragment();
        return findFragment;
    }

    @Override
    protected Class<FindDelegate> getDelegateClass() {
        return FindDelegate.class;
    }

    @Override
    protected void initData() {
        super.initData();
        findImgModel = new FindImgModelImpl();
        mNews = new ArrayList<>();
        mAdapter = new FindPictureAdapter(R.layout.item_fenlei, R.layout.item_headview, mNews);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                ToastUtils.showShort("11111111");
            }
        });
//        mAdapter.setOnRecyclerViewItemLongClickListener(new BaseQuickAdapter.OnRecyclerViewItemLongClickListener() {
//            @Override
//            public boolean onItemLongClick(View view, int i) {
//                ToastUtils.showShort("11111111");
//                return false;
//            }
//        });

//        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
////                netDataList(false);
//            }
//        });
        viewDelegate.setListAdapter(mAdapter);
        netDataList(true);
    }

    private void netDataList(boolean isRefresh) {
        if (isRefresh) {
            mPageNum = 1;
        } else {
            mPageNum++;
        }
        findImgModel.netLoadDataList(mPageNum, totalpage, 5, new OnNetRequestListener<ClassifyEntity>() {
            @Override
            public void onStart() {
                Logger.i("netLoadDataList", "onStart");
            }

            @Override
            public void onFinish() {
                Logger.i("netLoadDataList", "onFinish");

            }

            @Override
            public void onSuccess(ClassifyEntity data) {
                Logger.i("netLoadDataList", "onSuccess" + data.getData().toString());
                if (null != data) {
//                    mNews.addAll(data.getData());
                    List<ItemData> sampleData = DataSever.getSampleData(data);
                    mAdapter.notifyDataChangedAfterLoadMore(sampleData, true);
//                    mAdapter.notifyDataSetChanged();
                } else {
                    mAdapter.notifyDataChangedAfterLoadMore(false);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Logger.i("netLoadDataList", "onFailure");
            }
        });
    }
}
